package com.hyg.lib_music.ui.activity;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.hyg.lib_base.mainUtils.StatusBarUtil;
import com.hyg.lib_common.Base.BaseActivity;
import com.hyg.lib_common.DataModel.testting.TesttingAnswer;
import com.hyg.lib_common.DataModel.testting.TesttingXinliQuestion;
import com.hyg.lib_music.R;
import com.hyg.lib_music.adapter.RadioButtonAnswerAdapter;
import com.hyg.lib_music.databinding.ActivityTesttingXinliBinding;
import com.hyg.lib_common.listener.RCVchlickListener;

import java.util.ArrayList;

public class TesttingXinliActivity extends BaseActivity {
    ArrayList<TesttingXinliQuestion> list;
    int currentIndex = 0;
    ImageView BackIv;
    TextView titleTv, totalNumber, totalMin, progressTv, questionTv;
    RecyclerView answerRCV;
    Button BackOffBtn, NextBtn, FinishBtn;
    ActivityTesttingXinliBinding binding;
    RadioButtonAnswerAdapter adapter;
    public boolean isChooseFlag = false;

    Handler handler=new Handler();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityTesttingXinliBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        StatusBarUtil.setStatusBar(1, this, getResources().getColor(R.color.white), 60, null, null, true);
        init();
    }

    public void init() {
        initdata();
        initView();
    }

    public void initdata() {
        list = new ArrayList<>();
        list.add(new TesttingXinliQuestion("因为担忧而失眠", "没有,与平时差不多,比平时少些,比平时少很多"));
        list.add(new TesttingXinliQuestion("总是感到有压力", "没有,与平时差不多,比平时少些,比平时多很多"));
        list.add(new TesttingXinliQuestion("做事情时能够集中注意力", "比平时好,与平时一样,比平时差,比平时差很多"));
        list.add(new TesttingXinliQuestion("觉得自己在生活中是个有用的人", "比平时有用,与平时一样,比平时少,比平时少很多"));
        list.add(new TesttingXinliQuestion("能够敢于面对自己的问题", "比平时多一些,与平时一样,比平时差,比平时差很多"));
        list.add(new TesttingXinliQuestion("能够对事情做出决定", "比平时容易,与平时一样,比平时难,比平时难很多"));
        list.add(new TesttingXinliQuestion("感到无法克服困难", "没有,与平时差不多,比平时多些,比平时多很多"));
        list.add(new TesttingXinliQuestion("总的来说,心情还是愉快的", "比平时多一些,与平时一样,比平时少些,比平时少很多"));
        list.add(new TesttingXinliQuestion("能够享受日常生活", "比平时喜爱,与平时一样,不如平时喜爱,比平时差很多"));
        list.add(new TesttingXinliQuestion("感到情绪低落和压抑", "没有,与平时差不多,比平时多些,比平时多很多"));
        list.add(new TesttingXinliQuestion("对自己失去信心", "没有,与平时差不多,比平时多些,比平时多很多"));
        list.add(new TesttingXinliQuestion("觉得自己是一个没用的人", "没有,与平时差不多,比平时多些,比平时多很多"));
        list.add(new TesttingXinliQuestion("近6个月感到特别紧张,焦虑", "从来没有,极少,有时,经常"));
        list.add(new TesttingXinliQuestion("您认为您的身体健康状况为", "非常好,比较好,好,一般,较差"));
        list.add(new TesttingXinliQuestion("您认为您的心理健康状况为", "非常好,比较好,好,一般,较差"));
    }


    public void initView() {
        binding.lnTitleView.tvTitle.setText("心里评估问卷");
        binding.lnTitleView.tvTitleMore.setText("问卷介绍");
        binding.lnTitleView.tvTitleMore.setVisibility(View.VISIBLE);
        binding.lnTitleView.tvTitleMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(TesttingXinliActivity.this, TesttingIntroActivity.class);
                intent.putExtra("type", "2");
                startActivity(intent);
            }
        });
        binding.lnTitleView.back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        binding.btnBackOff.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updataView(--currentIndex);
            }
        });
        binding.btnNextQuestion.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isChecked()) {
                    handler.removeCallbacksAndMessages(null);
                    updataView(++currentIndex);
                } else {
                    Toast.makeText(TesttingXinliActivity.this, "您尚未选择答案！", Toast.LENGTH_SHORT).show();
                }
            }
        });
        binding.btnFinishQuestion.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isChecked()) {
                    handler.removeCallbacksAndMessages(null);
                    Intent intent = new Intent(TesttingXinliActivity.this, TestingResultActivity.class);
                    intent.putExtra("type", "3");
                    intent.putExtra("result", parseResult());
                    startActivity(intent);
                    finish();
                } else {
                    Toast.makeText(TesttingXinliActivity.this, "您尚未选择答案！", Toast.LENGTH_SHORT).show();
                }
            }
        });
        initRecycleView();
        updataView(currentIndex);
    }

    public void updataView(int position) {
        if (list == null) return;
        binding.tvQuestionNumber.setText(list.size() + "");
        binding.tvQuestionTime.setText((list.size() / 6 + 1) + "");
        binding.tvQuestionIndex.setText((position + 1) + "/" + list.size());
        binding.tvQuestion.setText(list.get(position).getQuestion());
        if (position == 0) {
            binding.btnBackOff.setVisibility(View.GONE);
        } else {
            binding.btnBackOff.setVisibility(View.VISIBLE);
        }
        if (position == list.size() - 1) {
            binding.btnFinishQuestion.setVisibility(View.VISIBLE);
            binding.btnNextQuestion.setVisibility(View.GONE);
        } else {
            binding.btnFinishQuestion.setVisibility(View.GONE);
            binding.btnNextQuestion.setVisibility(View.VISIBLE);
        }
        adapter = new RadioButtonAnswerAdapter(this, list.get(currentIndex).getArrayList(),new RCVchlickListener() {
            @Override
            public void onItemClickListener() {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if(currentIndex<list.size()-1){
                            updataView(++currentIndex);
                        }
                    }
                }, 400);
            }
        });
        binding.rcvQuestion.setAdapter(adapter);
    }

    public void initRecycleView() {
        if (list == null) return;
        GridLayoutManager layoutManager = new GridLayoutManager(this, 1);
        adapter = new RadioButtonAnswerAdapter(this, list.get(currentIndex).getArrayList(),new RCVchlickListener() {
            @Override
            public void onItemClickListener() {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if(currentIndex<list.size()-1){
                            updataView(++currentIndex);
                        }
                    }
                }, 400);

            }
        });
        binding.rcvQuestion.setLayoutManager(layoutManager);
        binding.rcvQuestion.setAdapter(adapter);
    }


    public boolean isChecked() {
        for (TesttingAnswer answer : list.get(currentIndex).getArrayList()) {
            if (answer.isChoose())
                return true;
        }
        return false;
    }

    public int calculateScoreSingle(ArrayList<TesttingAnswer> list) {
        int sum = 0;
        for (int i = 0; i < list.size(); i++) {
            if (i >= 2 && list.get(i).isChoose()) {
                sum = 1;
                return sum;
            }
        }
        return sum;
    }

    public int calculateScoreTogether(int[] indexs, ArrayList<TesttingXinliQuestion> list) {
        int sum = 0;
        if (indexs[indexs.length - 1] >= list.size())
            return sum;
        for (int i = 0; i < indexs.length; i++) {
            sum += calculateScoreSingle(list.get(indexs[i]).getArrayList());
        }
        return sum;
    }

    public int calculateScoreTogether(ArrayList<TesttingXinliQuestion> list) {
        int sum = 0;
        for (TesttingXinliQuestion question : list) {
            sum += calculateScoreSingle(question.getArrayList());
        }
        return sum;
    }


    public String parseResult() {
        ArrayList<String> resultList = new ArrayList<>();
        String result="";
        int sum = calculateScoreTogether(list);
        if (calculateScoreSingle(list.get(7).getArrayList()) > 0 && sum >= 4) {
            resultList.add("抑郁");
        }
        if (calculateScoreSingle(list.get(1).getArrayList()) > 0 && sum >= 4 || calculateScoreSingle(list.get(12).getArrayList()) > 0) {
            resultList.add("焦虑");
        }
        if (resultList.size() == 0) {
            if (sum >= 4 || calculateScoreSingle(list.get(13).getArrayList()) > 0 || calculateScoreSingle(list.get(14).getArrayList()) > 0) {
                resultList.add("压力");
            } else {
                resultList.add("健康");
            }
        }
        for(String string:resultList){
            if(TextUtils.isEmpty(result)){
                result=string;
            }else{
                result=result+"、"+string;
            }
        }
        return result;
    }


    @Override
    public void finish() {
        super.finish();
        handler.removeCallbacksAndMessages(null);
    }
}